package org.wevil.repo.sys;

import org.wevil.entity.sys.SysUser;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.wevil.repo.BaseRepo;

import java.util.List;

public interface SysUserRepo extends BaseRepo<SysUser, Integer> {

    SysUser findByTel(String tel);

    SysUser findByEmail(String email);

    List<SysUser> findByIdNot(Integer userId);

    SysUser findByUsername(String username);

    SysUser findByUsernameAndPassword(String username, String password);

    @Modifying
    @Query("update SysUser t set " +
            "t.username = case when :#{#entity.username} is null then t.username else :#{#entity.username} end, " +
            "t.password = case when :#{#entity.password} is null then t.password else :#{#entity.password} end, " +
            "t.realname = case when :#{#entity.realname} is null then t.realname else :#{#entity.realname} end, " +
            "t.tel = case when :#{#entity.tel} is null then t.tel else :#{#entity.tel} end, " +
            "t.email = case when :#{#entity.email} is null then t.email else :#{#entity.email} end, " +
            "t.avatar = case when :#{#entity.avatar} is null then t.avatar else :#{#entity.avatar} end, " +
            "t.address = case when :#{#entity.address} is null then t.address else :#{#entity.address} end, " +
            "t.idcard = case when :#{#entity.idcard} is null then t.idcard else :#{#entity.idcard} end, " +
            "t.openid = case when :#{#entity.openid} is null then t.openid else :#{#entity.openid} end " +
            "where t.id = :#{#entity.id}")
    void update(SysUser entity);

    @Query("select username from SysUser where id in ?1")
    List<String> findUserNameByIdIn(List<Integer> userIds);
}
